package com.dev.module_ui_composable.view

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.fragment.app.Fragment
import com.dev.module_ui_composable.R

/**
 *
 * Fragment中使用 Compose
 *
 * ```
 * https://developer.android.google.cn/jetpack/compose/migrate/interoperability-apis/compose-in-views?hl=zh-cn
 * ```
 */
class ComposeSampleFragment : Fragment() {

    private lateinit var composeView: ComposeView

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_compose_sample, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        composeView = view.findViewById(R.id.sample_composeView)
        inflateComposeView()
    }

    private fun inflateComposeView() {
        composeView.apply {
            setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
            setContent {}
        }
    }

}